package cn.mandifal.staticpages.service.impl;

import cn.mandifal.mapper.DpGoodsDescMapper;
import cn.mandifal.mapper.DpGoodsMapper;
import cn.mandifal.mapper.DpItemCatMapper;
import cn.mandifal.mapper.DpItemMapper;
import cn.mandifal.pojo.DpGoods;
import cn.mandifal.pojo.DpItem;
import cn.mandifal.pojo.DpItemExample;
import cn.mandifal.staticpages.service.ItemPageService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品 sku 的页面详情生成服务实现
 * @author shun
 */
@Component
public class ItemPageServiceImpl implements ItemPageService {
    @Value("${pagePath}")
    private String pagePath;
    @Autowired
    private FreeMarkerConfig freeMarkerConfig;
    @Autowired
    private DpGoodsMapper dpGoodsMapper;
    @Autowired
    private DpGoodsDescMapper dpGoodsDescMapper;
    @Autowired
    private DpItemCatMapper dpItemCatMapper;
    @Autowired
    private  DpItemMapper dpItemMapper;

    @Override
    public boolean getItemHtml(Long goodsId) {
        //得到配置文件中配置的Configuration bean
        Configuration configuration=freeMarkerConfig.getConfiguration();
        try {
            Template template = configuration.getTemplate("item.ftl");
            //创建数据模型
            Map map=new HashMap();
            //1.放入商品 spu 数据
            DpGoods dpGoods = dpGoodsMapper.selectByPrimaryKey(goodsId);
            map.put("goods",dpGoods);
            //2.放入商品详情表数据
            map.put("goodsDesc",dpGoodsDescMapper.selectByPrimaryKey(goodsId));
            //3.放入三级分类数据
            String category1 = dpItemCatMapper.selectByPrimaryKey(dpGoods.getCategory1Id()).getName();
            String category2 = dpItemCatMapper.selectByPrimaryKey(dpGoods.getCategory2Id()).getName();
            String category3 = dpItemCatMapper.selectByPrimaryKey(dpGoods.getCategory3Id()).getName();
            map.put("category1",category1);
            map.put("category2",category2);
            map.put("category3",category3);
            //4.放入 sku 信息
            DpItemExample example=new DpItemExample();
            DpItemExample.Criteria criteria = example.createCriteria();
            criteria.andGoodsIdEqualTo(goodsId);
            criteria.andStatusEqualTo("1");
            //排序,设置第一条为默认 sku，方便前端拿取
            example.setOrderByClause("is_default desc");

            List<DpItem> dpItemList = dpItemMapper.selectByExample(example);
            map.put("dpItemList",dpItemList);
            //输出位置
            Writer out=new FileWriter(pagePath+goodsId+".html");
            System.out.println(pagePath+goodsId);
            template.process(map,out);
            out.close();
            return true;
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 删除商品 sku 详情页面
     * @param goodsIds 商品 spu id
     * @return 返回执行结果
     */
    @Override
    public boolean deleteItemHtml(Long[] goodsIds) {
        try {
            for(Long goodsId:goodsIds){
                new File(pagePath+goodsId+".html").delete();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

}
